package com.future;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author future
 * @create 2019-06-19
 */
public class WatermarkUtils {

    /**
     * 图片格式：JPG
     */

    private static final String PICTRUE_FORMATE_JPG = "jpg";

    public static void main(String[] args) throws IOException {
        BufferedImage originalImage = ImageIO.read(new File("E:/project/java/inco.png"));
        BufferedImage watermarkImage = ImageIO.read(new File("E:/project/java/usermm1.jpg"));

//        BufferedImage thumbnail = Thumbnails.of(originalImage).watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
//                .asBufferedImage();

        BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(0, 0, 10, 10);
        char[] data = "liudTest".toCharArray();
        g.drawChars(data, 0, data.length, 5, 32);
        // watermark 参数 1.位置 2.水印图片 3.透明度.scale(1.0f)
        Thumbnails.of(watermarkImage).size(800, 800).watermark(Positions.CENTER, bi, 0.5f).toFile("E:/project/java/image_warter.jpg");
        String tbr = "投保人：王探";
        String bdw = "标的物：小麦";

        pressText("E:/project/java/1.jpg",tbr,"宋体",Font.ROMAN_BASELINE,30,Color.RED,60,40,1F);
    }

    /**
     * 添加文字水印
     *
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：中国证券网
     * @param fontName  字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize  字体大小，单位为像素
     * @param color     字体颜色
     * @param x         水印文字距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y         水印文字距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha     透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     */

    public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha) {

        try {

            File file = new File(targetImg);
            Image image = ImageIO.read(file);
            //图片宽度
            int width = image.getWidth(null);
            //图片高度
            int height = image.getHeight(null);

            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(new Font(fontName, fontStyle, fontSize));
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            //获取文字的宽度
            int width_1 = fontSize * getLength(pressText);
            //判断剩余可移动的宽度
            int widthDiff = width - width_1;
            int heightDiff = height - fontSize;
            //如果是正的就从左边开始，负的从右边开始
            int realX = 0;
            if(x>0){
                if(x > widthDiff){
                    realX = widthDiff;
                }else{
                    realX = x;
                }
            }

            int realY = 0;
            if(y>0){
                if (y > heightDiff) {
                    realY = heightDiff;
                }else{
                    realY= y;
                }
            }
            g.drawString(pressText, realX, realY + fontSize);
            g.dispose();
            ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * https://www.qi788.com/info-43.html
     * <p>
     * 获取字符长度，一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
     *
     * @param text
     * @return 字符长度，如：text="中国",返回 2；text="test",返回 2；text="中国ABC",返回 4.
     */

    public static int getLength(String text) {

        int textLength = text.length();

        int length = textLength;

        for (int i = 0; i < textLength; i++) {

            if (String.valueOf(text.charAt(i)).getBytes().length > 1) {

                length++;

            }

        }

        return (length % 2 == 0) ? length / 2 : length / 2 + 1;

    }

}